﻿using BW.WMS.Application.Features.Bins.Dtos;
using BW.WMS.Application.Features.Location.Dtos;
using BW.WMS.Application.Features.Location.Interfaces;
using BW.WMS.Core.Enums;
using BW.WMS.Core.Result;
using BW.WMS.Infrastructure.Data.Contexts;
using Dapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.ML;
using System.Text;

namespace BW.WMS.API.Controllers
{
    /// <summary>
    /// WMS仓库管理—库位控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WMS_LocationController : ControllerBase
    {
        private readonly IWMS_LocationService locationService;
        /// <summary>
        /// 构造函数注入库位服务接口
        /// </summary>
        /// <param name="locationService">库位服务接口</param>
        public WMS_LocationController(IWMS_LocationService locationService)
        {
            this.locationService = locationService;
        }
        /// <summary>
        /// 获取库位分页列表，支持多条件查询
        /// </summary>
        /// <param name="queryDto">查询条件</param>
        /// <returns>返回查询分页结果</returns>
        [HttpGet]
        public async Task<ApiPage<WMS_LocationDto>> GetLocationPageListAsync([FromQuery]WMS_LocationQueryDto queryDto)
        {
            return await locationService.GetLocationPageListAsync(queryDto);
        }
        /// <summary>
        /// 根据ID获取库位详情，包括库位基本信息和下属货位列表
        /// </summary>
        /// <param name="id">库位ID</param>
        /// <returns>库位详情DTO，包含基本信息和货位集合</returns>
        [HttpGet]
        public async Task<ApiResult<WMS_LocationDto>> GetLocationByIdAsync([FromQuery]long id)
        {
            return await locationService.GetLocationByIdAsync(id);
        }
        /// <summary>
        /// 创建新库位
        /// </summary>
        /// <param name="createDto">库位创建DTO</param>
        /// <returns>操作结果，成功时返回新创建的库位ID</returns>
        [HttpPost]
        public async Task<ApiResult<long>> CreateLocationAsync(WMS_LocationCreateDto createDto)
        {
            return await locationService.CreateLocationAsync(createDto);
        }
        /// <summary>
        /// 删除库位（逻辑删除）
        /// </summary>
        /// <param name="id">库位ID</param>
        /// <returns>操作结果，表示删除是否成功</returns>
        [HttpDelete]
        public async Task<ApiResult<bool>> DeleteLocationAsync(long id)
        {
            return await locationService.DeleteLocationAsync(id);
        }

        /// <summary>
        /// 批量删除库位（逻辑删除）
        /// </summary>
        /// <param name="ids">库位ID列表</param>
        /// <returns>操作结果，表示删除是否成功</returns>
        [HttpDelete]
        public async Task<ApiResult<bool>> BatchDeleteLocationsAsync(List<long> ids)
        {
            return await locationService.BatchDeleteLocationsAsync(ids);
        }
        /// <summary>
        /// 更新库位信息
        /// </summary>
        /// <param name="updateDto">库位更新DTO</param>
        /// <returns>操作结果，表示更新是否成功</returns>
        [HttpPut]
        public async Task<ApiResult<bool>> UpdateLocationAsync(WMS_LocationUpdateDto updateDto)
        {
            return await locationService.UpdateLocationAsync(updateDto);
        }
        /// <summary>
        /// 更新库位状态
        /// </summary>
        /// <param name="id">库位ID</param>
        /// <param name="status">新状态值</param>
        /// <param name="updatedBy">操作人</param>
        /// <returns>操作结果，表示状态更新是否成功</returns>
        [HttpPut]
        public async Task<ApiResult<bool>> UpdateLocationStatusAsync(long id, int status, string updatedBy)
        {
            return await locationService.UpdateLocationStatusAsync(id, status, updatedBy);
        }
    }
}
